package com.drug.service.impl;


import com.drug.domain.Result;
import com.drug.domain.User;
import com.drug.domain.UserR;
import com.drug.domain.UserRole;
import com.drug.mapper.RoleMenuinfoMapper;
import com.drug.mapper.UserMapper;
import com.drug.mapper.UserRoleMapper;
import com.drug.service.UserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private  UserMapper userMapper;
    @Autowired
    private RoleMenuinfoMapper roleMenuinfoMapper;
    private UserR userR;

    //多条件查询
    @Override
    public List<UserR> selectUser(UserR userR) {
        //用户名
        if (userR.getUName()!=null&&userR.getUName()!=""){
            String uname = userR.getUName();
            userR.setUName(uname.toString());
        }else{
            userR.setUName(null);
        }
         //用户类型
        if (userR.getRId()!=null||userR.getRId().equals("")){
            Integer rId=0;
            rId  = userR.getRId();
            if (rId==0){
                    rId=1;
                userR.setRId(rId);
            }else if (rId==1){
                rId=2;
                userR.setRId(rId);
            }else if (rId==1){
                rId=2;
                userR.setRId(rId);
            }else if (rId==2){
                rId=3;
                userR.setRId(rId);
            }else if (rId==3){
                rId=4;
                userR.setRId(rId);
            }else if (rId==4){
                rId=5;
                userR.setRId(rId);
            }
        }else{
            userR.setRId(null);
        }
        //邮编
        if (userR.getUPostalcode()!=null&&userR.getUPostalcode()!=""){
            String postalcode = userR.getUPostalcode();
            userR.setUPostalcode(postalcode.toString());
        }else{
            userR.setUPostalcode(null);
        }
        //用户状态
        if (userR.getUState()!=null||userR.getUState().equals("")){
            String uState = userR.getUState();
            userR.setUState(uState);
        }else{
            userR.setUState(null);
        }
        //联系人
        if (userR.getUContacts()!=null&&userR.getUContacts()!=""){
            String uContacts = userR.getUContacts();
            userR.setUContacts(uContacts.toString());
        }else{
            userR.setUContacts(null);
        }
        //email
        if (userR.getUEmail()!=null&&userR.getUEmail()!=""){
            String uContacts = userR.getUEmail();
            userR.setUEmail(uContacts.toString());
        }else{
            userR.setUEmail(null);
        }
        return userRoleMapper.selectUser(userR);
    }


    //逻辑杀出
    @Override
    public void deleteUser(Integer uId) {
       userRoleMapper.deleteUser(uId);
    }
    //新增
    @Override
    public void insertUser(UserR userR) {
        String uName = userR.getUName();
        Integer rId = userR.getRId();
//        String rName = userR.getRName();
//        int rname = Integer.parseInt(rName);
        String password = userR.getUPassword();
//        String uState = userR.getUState();
        String uCompany = userR.getUCompany();
        String uAdress = userR.getUAdress();
        String postalcode = userR.getUPostalcode();
        String uContacts = userR.getUContacts();
        String uPhone = userR.getUPhone();
        String email = userR.getUEmail();
        User user = new User();
        user.setuName(uName);
        user.setuPassword(password);
        user.setuAdress(uAdress);
        user.setuCompany(uCompany);
        user.setuPostalcode(postalcode);
        user.setuPhone(uPhone);
        user.setuContacts(uContacts);
        user.setuEmail(email);
        Integer integer = userMapper.addUser(user);
        Integer uid = user.getuId();
        UserRole userRole = new UserRole();
        userRole.setUId(uid);
        userRole.setRId(rId);
        System.err.println(userRole);
        int i = userRoleMapper.insertUser(userRole);
    }

    //修改
    @Override
    public Integer updateUser(UserR userR) {
        int i = userRoleMapper.updateUser(userR);
        int a=userMapper.updateUser(userR);
        return i;
    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @Override
    public Result findAll(Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result<>();
        List<User> users = userRoleMapper.selectPageUser();
        PageInfo<User> pageInfo= new PageInfo<User>(users);
        result.setData(pageInfo);
        return result;
    }

    @Override
    public Result pdRole(String username) {
        Result<Object> objectResult = new Result<>();
        objectResult.setData(username);
        String selectRole = roleMenuinfoMapper.selectRole(username);
        if (("ROLE_ADIMN").equals(selectRole)){
            objectResult.setCode(0);
        }
        if (("ROLE_BUREAU").equals(selectRole)){
            objectResult.setCode(1);
        }
        if (("ROLE_ROOM").equals(selectRole)){
            objectResult.setCode(2);
        }
        if (("ROLE_YARD").equals(selectRole)){
            objectResult.setCode(3);
        }
        if (("ROLE_SUPPLIER").equals(selectRole)){

            objectResult.setCode(4);
        }
        return objectResult;
    }


    //字典 用户类型  查询
    @Override
    public List<UserR> selectDictionaryUser(UserR userR) {
        //用户名
        if (userR.getUName()!=null&&userR.getUName()!=""){
            String uname = userR.getUName();
            userR.setUName(uname.toString());
        }else{
            userR.setUName(null);
        }
        //用户类型
        if (userR.getRId()!=null||userR.getRId().equals("")){
            Integer rId=0;
            rId  = userR.getRId();
            if (rId==0){
                rId=1;
                userR.setRId(rId);
            }else if (rId==1){
                rId=2;
                userR.setRId(rId);
            }else if (rId==1){
                rId=2;
                userR.setRId(rId);
            }else if (rId==2){
                rId=3;
                userR.setRId(rId);
            }else if (rId==3){
                rId=4;
                userR.setRId(rId);
            }else if (rId==4){
                rId=5;
                userR.setRId(rId);
            }
        }else{
            userR.setRId(null);
        }
        //邮编
        if (userR.getUPostalcode()!=null&&userR.getUPostalcode()!=""){
            String postalcode = userR.getUPostalcode();
            userR.setUPostalcode(postalcode.toString());
        }else{
            userR.setUPostalcode(null);
        }
        //用户状态
        if (userR.getUState()!=null||userR.getUState().equals("")){
            String uState = userR.getUState();
            userR.setUState(uState);
        }else{
            userR.setUState(null);
        }
        //联系人
        if (userR.getUContacts()!=null&&userR.getUContacts()!=""){
            String uContacts = userR.getUContacts();
            userR.setUContacts(uContacts.toString());
        }else{
            userR.setUContacts(null);
        }
        //email
        if (userR.getUEmail()!=null&&userR.getUEmail()!=""){
            String uContacts = userR.getUEmail();
            userR.setUEmail(uContacts.toString());
        }else{
            userR.setUEmail(null);
        }
        return userRoleMapper.selectUser(userR);
    }
}
